using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbTrangThaiDHDAL
    {
        private SqlConnection cn = null;
        public tbTrangThaiDHDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTrangThaiDH ttdh)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiDH_insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTrangThaiDH ", ttdh.IdTrangThaiDH);
            cm.Parameters.AddWithValue("@TrangThaiDH", ttdh.TrangThai_DH);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTrangThaiDH ttdh)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiDH_update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTrangThaiDH ", ttdh.IdTrangThaiDH);
            cm.Parameters.AddWithValue("@TrangThaiDH ", ttdh.TrangThai_DH);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiDH_delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTrangThaiDH", Id);
            cm.ExecuteNonQuery();
        }
        public tbTrangThaiDHCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiDH_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbTrangThaiDHCollection ttdhcollection = new tbTrangThaiDHCollection();
            while (dr.Read())
            {
                tbTrangThaiDH ttdh = new tbTrangThaiDH();
                ttdh.IdTrangThaiDH = int.Parse(dr.GetValue(0).ToString());
                ttdh.TrangThai_DH =  bool.Parse (dr.GetValue(1).ToString());
                ttdhcollection.Add(ttdh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttdhcollection;
        }
    }
}
